package com.taotao.tools.jackson;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

/**
 * 描述信息
 *
 * @author youbeiwuhuan
 * @version 1.0
 * @date 2020/9/7 17:13
 */
public class Jackson {


    public static void main(String[] args) throws IOException {
        //ObjectMapper本身是线程安全的，全局只建一个就够了
        ObjectMapper mapper=new ObjectMapper();
        //序列化时不包含空属性
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        //反序列化时忽略未知的属性
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        String json="{\"name\":\"zcq\",\"age\":1,\"birthday\":12}";
        Person person=mapper.readValue(json, Person.class);

        System.out.println(person);

        String json2=mapper.writeValueAsString(person);

        System.out.println(json2);

    }
}
